<template>
    <div class="phone">
        <cube-scroll>
            <div id="hint" v-if="phone === '' && $route.name === 'index-phone'">完成手机认证后可开红包</div>
            <form id="form">
                <cube-input
                   class="child"
                   placeholder="请输入手机号"
                   v-model="phone"
                ></cube-input>

<!--                <cube-input-->
<!--                    class="child code"-->
<!--                    placeholder="请输入验证码"-->
<!--                >-->
<!--                </cube-input>-->
<!--                <cube-button-->
<!--                    class="code-button"-->
<!--                    :inline="true">-->
<!--                    获取验证码-->
<!--                </cube-button>-->
                <cube-button @click="submit">
                    认证
                </cube-button>
            </form>
        </cube-scroll>
    </div>
</template>

<script>
import store from "../../../store/index"
export default {
    name: "phone",
    data () {
        return {
            setting:[],
            phone: ""
        }
    },
    computed:{
      data () {
          return {
              phone: this.phone
          }
      }
    },
    created(){
        this.getSetting()
            .then(() => {
                if(this.setting["open_phone"] === "1"){
                    this.get();
                } else {
                    this.$router.go(-1);
                }
            })
    },
    methods: {
        /**
         * 获取参数配置
         */
        getSetting () {
            return this.$url("setting/easyFind",{
                return: ["open_phone"]
            })
                .then((res) => {
                    if(res.data.status === "success"){
                        this.setting = res.data.data;
                    }
                })
        },
        /**
         * 获取用户信息
         */
        get(){
            this.$url("memberA/find")
                .then((res) => {
                    let { data, status } = res.data
                    if( status === "success") {
                        this.phone = data.phone
                    }
                })
        },
        /**
         * 提交修改用户手机号
         */
        submit() {
            if(!/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phone)) {
                this.$createToast({
                    type: "error",
                    txt: '请设置正确的手机号码',
                    mask: true
                }).show();
                return;
            }
            this.$url("memberA/set",{
                data:this.data
            })
                .then((res) => {
                    if( res.data.status === "success") {
                        store.commit("setPhone",this.phone);
                        if(this.$route.name === "index-phone"){
                            this.$router.go(-1);
                        }else{
                            this.$createToast({
                                type:"correct",
                                time: 1000,
                                txt: '操作成功',
                                mask: true
                            }).show();
                        }
                    } else {
                        this.$createToast({
                            type:"error",
                            time: 1000,
                            txt: '操作失败',
                            mask: true
                        }).show();
                    }
                })
        }
    }
}
</script>

<style scoped lang="less" src="./phone.less"></style>
